package com.bootdo.loa.mv;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

import javax.imageio.stream.FileImageInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.R;
import com.bootdo.loa.domain.TbMediaDO;
import com.bootdo.loa.service.TbMediaService;
import com.bootdo.system.domain.CompanyDO;
import com.bootdo.system.domain.CompanyPictureDO;

@RequestMapping("/mv/io")
@Controller
public class IoUpload {
           
	  private static String bucketName = "suyongmmv";
	  @Autowired
	  TbMediaService tbMediaService;
	  
	  
	@GetMapping()
	String io(){
	    return "uploads/newfile";
	}
	
	
	@ResponseBody
	@PostMapping("/upload")
	public R addAdvertisingPicture(@RequestParam("img") MultipartFile  img) {
		String URL = null;
		try {
			URL = OssUtil.fileUploadByName(img,bucketName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.ok(URL);
	}
	
	@ResponseBody
	@PostMapping("/uploadnews")
	public R uploadnews(@RequestParam("file") MultipartFile  file) {
		String URL = null;
		try {
			URL = OssUtil.fileUploadByName(file,bucketName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.ok(URL);
	}
	
	
	@ResponseBody
	@PostMapping("/savePic")
	public R savePic(@RequestParam("img") MultipartFile[] img){
		 try {
				for (MultipartFile file : img) {
					String url =null;
					try {
						url = OssUtil.fileUpload(file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					TbMediaDO tbMediaDO = new TbMediaDO();
					tbMediaDO.setPicture(url);
					tbMediaDO.setTags("pic");
					tbMediaService.save(tbMediaDO);
				}
			return R.ok();
		} catch (Exception e) {
			return R.error();
		}
		
			
			
	}
	
	@ResponseBody
	@PostMapping("/editLogo")
	public R saveLogo(@RequestParam("img") MultipartFile img,Integer id){
		String imgUrl=tbMediaService.get(id).getPicture().substring(45, 62);
		OssUtil.deleteBucketInfo(imgUrl);
		String url =null;
		try {
			 url = OssUtil.fileUpload(img);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return R.error("网络异常");

		}
		return R.ok(url);

	
			
			
	}
	
	
	

	@GetMapping("/uploadimg")
	void rebate(HttpServletRequest request, HttpServletResponse response,String url) throws IOException {

        String url1 = "/var/uploaded_files/" +url.substring(url.lastIndexOf("/")+1);
		//通过路径得到一个输入流
		//  /var/uploaded_files/
				FileInputStream fis = new FileInputStream(url1);
				
				//创建字节输出流
				ServletOutputStream sos = response.getOutputStream();
				
				//得到要下载的文件名
				String filename = url.substring(url.lastIndexOf("/")+1);   // "\\" == "/" 第一个"\"用于转意
		 		
				//设置文件名的编码
				filename = URLEncoder.encode(filename, "UTF-8");
				
				//告知客户端要下载的文件
				response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));

				response.setHeader("content-disposition", "attachment;filename=" + filename);
				response.setHeader("content-type", "image/jpeg");   //jpeg的简写为jpg   Content-type:下载文件类型
				
				//执行输出操作
				int len = 1;
				byte[] b = new byte[1024];
				while((len = fis.read(b)) != -1) {
					sos.write(b, 0, len);
				}
				
				/*方法内可以不关流*/
				sos.close();
				fis.close();
				
	}

	
	
	

	
	 
	
	
}
